package com.heitian.ssm.controller;



import com.google.gson.JsonObject;
import com.heitian.ssm.utils.*;
import com.qiniu.util.Auth;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者： 陈建峰
 * 创建时间： 18/3/19 下午9:06
 * 版本： [1.0, 18/3/19]
 * 描述： <描述>
 */
@Controller
@RequestMapping("/qiniufile")
public class QiNiuController {

    private Logger log = Logger.getLogger(QiNiuController.class);

    //---------------------------   七牛云配置数据开始    -----------------------------
    @Value("#{settings['accessKey']}")
    private String accessKey = "";

    @Value("#{settings['secretKey']}")
    private String secretKey = "";

    @Value("#{settings['bucket']}")
    private String bucket = "";
    //---------------------------   七牛云配置数据结束    -----------------------------

    //---------------------------   微信小程序的数据开始    -----------------------------
    /**
     * 微信小程序的数据
     */
    @Value("#{settings['grant_type']}")
    private String grant_type = "";

    @Value("#{settings['requestUrl']}")
    private String requestUrl = "";

    @Value("#{settings['appid']}")
    private String appid = "";

    @Value("#{settings['appsecret']}")
    private String appsecret = "";

    //---------------------------   微信小程序的数据结束    -----------------------------

    /**
     * 获取七牛云的token
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/GetUpToken",method= RequestMethod.POST)
    public void GetUpToken(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        //获取七牛云的token
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        log.info(upToken);
        Map<String,String> map = new HashMap<String, String>();
        map.put("upToken",upToken);
        response.getWriter().write(CommonUtils.createJson("1", "", GsonUtil.GsonString(map)).toString());
    }

    /**
     * 获取七牛云的token
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getUpToken",method= RequestMethod.GET)
    public void getUpToken(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //获取七牛云的token
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        log.info(upToken);
        Map<String,String> map = new HashMap<String, String>();
        map.put("uptoken",upToken);
        response.getWriter().write(GsonUtil.GsonString(map));
    }

    /**
     * 获取微信Openid
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/GetOpenid",method= RequestMethod.POST)
    public void GetOpenid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String js_code = paras.get("js_code").getAsString();

        if(js_code == null ||js_code.equals("")){
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }

        //请求参数
        String params = "appid=" + appid + "&secret=" + appsecret + "&js_code=" + js_code + "&grant_type=" + grant_type;
        //发送请求
        String data = HttpUtil.get(requestUrl, params);
        if(!CheckUtil.checkNulls(data)){
            String UserArea = data;
            String code = "1";
            String message = "";
            response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
        }else{
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
        }
    }
}
